import { createStore } from "vuex";

// 创建一个新的 store 实例
const store = createStore({
  state() {
    return {
      count: 0,
      uploadFile: null,
      uploadFileId: null,
      price: 0,
      token: 0,
      info: null, //翻译后的结果
      historyArr: [], //历史记录
      status: false,
      translateStatus: 0, //翻译状态
      translateLoading: false, // 翻译loading
      hasPaid: false, // 是否支付
      menuClocked: true, // 菜单是否点击
      showMenu: true, // 菜单是否显示
      menuClass: "", // 菜单样式
    };
  },
  getters: {
    menuClocked: (state) => state.menuClocked,
    showMenu: (state) => state.showMenu,
    menuClass: (state) => state.menuClass,
  },
  mutations: {
    updatedUploadFile(state, url) {
      console.log("url: ", url);
      state.uploadFile = url;
    },
    updatedUploadFileId(state, id) {
      state.uploadFileId = id;
    },
    updatePrice(state, price) {
      state.price = price;
    },
    updateToken(state, token) {
      state.token = token;
    },
    updatedFileInfo(state, info) {
      if (!info) {
        state.translateStatus = 0;
        state.info = null;
        return;
      }
      state.translateStatus = info.status;
      state.info = info;
    },
    updateHistoryArr(state, arr) {
      state.historyArr = arr;
    },
    updateShowForm(state, status) {
      state.status = status;
    },
    updateTranslateStatus(state, status) {
      state.translateStatus = status;
    },
    updateTranslateLoading(state, status) {
      state.translateLoading = status;
    },
    updateHasPaid(state, status) {
      state.hasPaid = status;
    },
    updateMenuClocked(state, status) {
      state.menuClocked = status;
    },
    updateShowMenu(state, res) {
      if (res) {
        state.menuClass = "show-menu";
      } else {
        state.menuClass = "hide-menu";
      }
      if (res) {
        state.showMenu = true;
      }
      setTimeout(() => {
        state.showMenu = res;
        state.menuClass = "";
      }, 300);
    },
  },
  actions: {
    reset({ commit, state }) {
      commit("updatedUploadFile", null);
      commit("updatedUploadFileId", null);
      commit("updatePrice", 0);
      commit("updateToken", null);
      commit("updatedFileInfo", null);
      commit("updateShowForm", true);
      commit("updateTranslateStatus", 0);
      commit("updateTranslateLoading", false);
    },
    updatedUploadFile({ commit, state }, payload) {
      commit("updatedUploadFile", payload);
      commit("updatedUploadFileId", payload.file_id);
      if (payload.gpt_version_list) {
        commit("updatePrice", payload.gpt_version_list[0].price);
      }
      commit("updateToken", payload.tokens_number);
      commit("updateShowForm", true);
    },
    updatedFileInfo({ commit, state }, payload) {
      commit("updatedFileInfo", payload);
    },
    updatedHistoryArr({ commit, state }, payload) {
      commit("updateHistoryArr", payload);
    },
    updatedTranslateStatus({ commit }, payload) {
      commit("updateTranslateStatus", payload);
    },
  },
});

export default store;
